﻿// StaffInfoMan.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <stdio.h>
#include "StaffItem.h"
#include "StaffIO.h"
#define PARTICIPANTS "袁顾阳  石鑫杰  张程斌"

enum Sex;
enum EducateLevel;
StaffItemHandler* han;

void add();//增
void search();//查
void remove();//删
void modify();//改
void save();//存
void read();//取

int main()
{
	han = new StaffItemHandler();
	printf("教职工信息管理系统 by ");
	printf(PARTICIPANTS);
	printf("\n\n");
	while (true) {
		printf("1=增 2=删 3=查 4=改 5=存盘 6=读档\n>");
		int input = 0;
		scanf_s("%d", &input);
		switch (input) {
		case 1:
			add();
			break;
		case 2:
			remove();
			break;
		case 3:
			search();
			break;
		case 4:
			modify();
			break;
		case 5:
			save();
			break;
		case 6:
			read();
			break;
		default:
			printf("[操作无效]\n");
			break;
		}
		printf("\n\n");
	}
}



void add() {
	StaffItem* current = StaffItemHandler::CreateStaffItem();
	printf("模式：增\n");
	current->id = intinput("ID(int)");
	current->age = intinput("年龄(int)");
	strinput("姓名(string)", current->name);
	current->sex = sexinput();
	current->edu = eduinput();
	han->InsertStaffItemAtTail(current);
}

void remove() {
	printf("模式：删\n");
	int id = intinput("ID(int)");
	StaffItem* item = han->PopStaffItemById(id);
	printf("已删除信息：");
	printf(item->name);
}

void search() {
	printf("模式：查\n");
	int id = intinput("ID(int)");
	StaffItem* item = han->GetStaffItemById(id);
	printf("信息：\n姓名:");
	printf("%s\n", item->name);
	printf("年龄:");
	printf("%d\n", item->age);
	printf("性别:");
	printSex(item->sex); printf("\n");
	printf("学历:");
	printEdu(item->edu); printf("\n");
}

void modify() {
	printf("模式：改\n");
	int id = intinput("ID(int)");
	StaffItem* item = han->GetStaffItemById(id);
	printf("信息：\n姓名:");
	printf("%s\n", item->name);
	printf("年龄:");
	printf("%d\n", item->age);
	printf("性别:");
	printSex(item->sex); printf("\n");
	printf("学历:");
	printEdu(item->edu); printf("\n");

	printf("\n\n输入新的信息：");
	item->id = intinput("ID(int)");
	item->age = intinput("年龄(int)");
	strinput("姓名(string)", item->name);
	item->sex = sexinput();
	item->edu = eduinput();
}

void save() {
	printf("正在存档...");
	FILE* fp = fopen("save.dat", "wb");
	if (fp == NULL)
	{
		printf("无法打开文件\n");
		return;
	}
	han->PointerReset();
	StaffItem* item = han->GetNextStaffItem();
	int cnt = 0;
	do {
		fwrite(item, sizeof(struct StaffItem), 1, fp);
		item = han->GetNextStaffItem();
		cnt++;
	} while (item != NULL);
	fclose(fp);
	printf(" 写出了%d条记录\n", cnt);
}

void read() {
	printf("正在读档...");
	FILE* fp = fopen("save.dat", "rb");
	if (fp == NULL)
	{
		printf("无法打开文件\n");
		return;
	}
	int res;
	int cnt = 0;
	StaffItem* item = StaffItemHandler::CreateStaffItem();
	res = fread(item, sizeof(struct StaffItem), 1, fp);
	do {
		han->InsertStaffItemAtTail(item);
		cnt++;
		item = StaffItemHandler::CreateStaffItem();
		res = fread(item, sizeof(struct StaffItem), 1, fp);
	} while (res == 1);
	fclose(fp);
	printf(" 读入了%d条记录\n", cnt);
}